package com.hf.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hf.dao.UserDao;
import com.hf.entity.User;
import com.hf.utils.CookieEncrypTool;

@WebServlet("/user.do")
public class UserServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		PrintWriter out = resp.getWriter();
		String type = req.getParameter("type");
		String remenber = req.getParameter("rem");
		User user1 = new User();
		user1 = getDateUser(req);
		User user = new User();
		UserDao userdao = new UserDao();
		HttpSession session = req.getSession();
		if("login".equals(type)){
			user = userdao.login(user1.getUser_login(), user1.getUser_password());
			if(user.getUser_id()!=0){
				rememberme(user1.getUser_login(),user1.getUser_password(),remenber,req,resp);			
				session.setAttribute("user_name", user1.getUser_login());
				session.setAttribute("user_id", user.getUser_id());
				session.setAttribute("user", user);
				req.getRequestDispatcher("index.jsp").forward(req, resp);
			}else{
				out.print("<script type='text/javascript'>");
				out.print("alert('账号或者密码错误!!!');");
				out.print("window.location.href='login.jsp';");
				out.print("</script>");
			}
		}else if("register".equals(type)){
			String code = req.getParameter("code");
			String yzm = (String) session.getAttribute("code1");
			if(yzm.equals(code)){				
				if(!userdao.isExitName(user1.getUser_name())){
					out.print("<script type='text/javascript'>");
					out.print("alert('用户名已经存在!!!');");
					out.print("window.location.href='register.jsp';");
					out.print("</script>");
				}else{
					userdao.InsertUser(user1);
					resp.sendRedirect("login.jsp");
				}
			}else{
				out.print("<script type='text/javascript'>");
				out.print("alert('验证码错误！');");
				out.print("window.location.href='register.jsp';");
				out.print("</script>");
			}
		}else if("loginout".equals(type)){
			session.invalidate();
			resp.sendRedirect("index.jsp");
		}
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	
	private User getDateUser(HttpServletRequest req){
		String user_name = req.getParameter("user_name");
		String user_login = req.getParameter("user_login");
		String user_password = req.getParameter("user_password");
		User user = new User(user_name,user_login,user_password);
		return user;
	}
	
	private void rememberme(String email, String password, String remenber, HttpServletRequest req,
			HttpServletResponse resp) {
		// TODO Auto-generated method stub
		
		if("true".equals(remenber)){
			
			Cookie cookieemail = new Cookie("Cookie_rememberemail",CookieEncrypTool.encdeBase64(email));
			cookieemail.setPath("/");
			cookieemail.setMaxAge(7*24*3600);
			resp.addCookie(cookieemail);
			
			Cookie cookiepwd = new Cookie("Cookie_rememberpwdl",CookieEncrypTool.encdeBase64(password));
			cookiepwd.setPath("/");
			cookiepwd.setMaxAge(7*24*3600);
			resp.addCookie(cookiepwd);		
		}else{
			Cookie[] cookie = req.getCookies();
			if(cookie!=null){
				for(Cookie cookielist:cookie){
					if("Cookie_rememberemail".equals(cookielist.getName()) || "Cookie_rememberpwdl".equals(cookielist.getName())){
						cookielist.setMaxAge(0);
						cookielist.setPath("/");
						resp.addCookie(cookielist);
					}
				}
			}
		}
		
	}
}
